﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Node.behaviourEditor;

namespace Node
{
    [CreateAssetMenu]
    public class BehaviourGraph : ScriptableObject
    {
        public List<Saved_StatesNode> savedStateNodes = new List<Saved_StatesNode>();
        Dictionary<StateNode, Saved_StatesNode> stateNodeDict = new Dictionary<StateNode, Saved_StatesNode>();
        Dictionary<State, StateNode> stateDict = new Dictionary<State, StateNode>();

        public void Init()
        {
            stateNodeDict.Clear();
            stateDict.Clear();
        }

        public void SetNode(BaseNode node)
        {
            if(node is StateNode)
            {
                SetStateNode((StateNode)node);
            }
            if (node is TransitionNode)
            {
            }
            if (node is CommentNode)
            {
            }
        }

    #region StateNode
    public void SetStateNode(StateNode node)
    {
        Saved_StatesNode s = GetSavedState(node);
        if(s==null)
        {
            s = new Saved_StatesNode();
            savedStateNodes.Add(s);
            stateNodeDict.Add(node, s);
        }

        s.state = node.currentState;
        s.position = new Vector2(node.windowRect.x, node.windowRect.y);
            s.isCollapsed = node.collapse;
    }

    public void ClearStateNode(StateNode node)
    {
        Saved_StatesNode s = GetSavedState(node);
        if(s != null)
        {
            savedStateNodes.Remove(s);
            stateNodeDict.Remove(node);
        }
    }

    Saved_StatesNode GetSavedState(StateNode node)
    {
        Saved_StatesNode r = null;
        stateNodeDict.TryGetValue(node, out r);
        return r;
    }

    public StateNode GetStateNode(State state)
    {
        StateNode r = null;
        stateDict.TryGetValue(state, out r);
        return r;
    }
#endregion
    }

    [System.Serializable]
    public class Saved_StatesNode
    {
        public State state;
        public Vector2 position;
        public bool isCollapsed;
    }

    [System.Serializable]
    public class Saved_Transition
    {

    }
}